package com.k.generator;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 数据库表名转换为xml For Mybatis generator
 * generatorConfig.xml中生成 table标签
 * @author KK
 *
 */
public class Kjdbc1 {
	public static void main(String[] args) {
		// set postgresql mysql oracle
		String db = "sqlite";
		String sqlite = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name; ";
		String mySQL = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'sop'";
		String postgreSQL = "SELECT tablename FROM pg_tables where schemaname = 'public';";
		// select * from user_tables;  or SELECT TABLE_NAME from all_tables where owner='SZYJ'
		String oracleSQL = "SELECT TABLE_NAME from all_tables where owner='SZYJ'";

		String sql =oracleSQL;

		// Go
		Connection conn = getConn(db);
		PreparedStatement ps = null;
		ResultSet rs=null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()){
				String rsname=rs.getString(1);
				String rsnameLower = rsname.toLowerCase();
				String[] names = (rsnameLower.trim().split("_"));
				String convertName="";
				//以_截取的串，每个串，首字母大写，如user_role -> UserRole
				for(String name:names){
					convertName+=name.substring(0, 1).toUpperCase()+name.substring(1) ;
				}
				//System.out.println("rsname:"+rsname+" convertName:"+convertName);
				String content = "<table tableName=\""+rsnameLower+"\" domainObjectName=\""+convertName+"\" "
						+"enableCountByExample=\"false\" enableSelectByExample=\"false\" "
						+"enableDeleteByExample=\"false\" enableUpdateByExample=\"false\">"
						+"<property name=\"useActualColumnNames\" value=\"false\" />"
						+ "</table>";
				System.out.println(content);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			if(ps!=null){
				try {
					ps.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}
	}

	private static Connection getConn(String whatDB) {
		String driver = null;
		String url = null;
		String user = null;
		String password = null;
		if ("ORACLE".equals(whatDB.toUpperCase())) {
			driver = "oracle.jdbc.driver.OracleDriver";
			url = "jdbc:oracle:thin:@localhost:1521:orcl";
			user = "root";
			password = "hitdybyandrew";
			url = "jdbc:oracle:thin:@172.16.0.198:1521:stsjcq";
			//szyj
			user = "data_swap";
			password = "data_swap";
		} else if ("MYSQL".equals(whatDB.toUpperCase())) {
			driver = "com.mysql.jdbc.Driver";
			//jdbc:mysql://localhost:3306/sop?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
			url="jdbc:mysql://localhost:3306/sop?useUnicode=true&amp;characterEncoding=utf-8";
			user = "root";
			password = "123456";
		} else if("POSTGRESQL".equals(whatDB.toUpperCase())){
			driver = "org.postgresql.Driver";
			url = "jdbc:postgresql://172.16.0.199:5432/smpnsyj";
			url = "jdbc:postgresql://172.16.0.199:5432/smpliuzhou0902";
			user = "smp";
			password = "smp";
		}else if ("SQLSERVER".equals(whatDB.toUpperCase())) {
			driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
			url = "jdbc:sqlserver://127.0.0.1:1443;databaseName=ktest;";
			user = "dyda";
			password = "hitdybyandrew";
		} else if("SQLITE".equalsIgnoreCase(whatDB.toUpperCase())){
			driver = "org.sqlite.JDBC";
			//jdbcUrl = "jdbc:sqlite::resource:static/sqllite/"+entity.getDbName();
			// jdbc:sqlite:c:/sqlite/test.db
			url = "jdbc:sqlite:c:\\gitrepo\\asbe\\kpp\\src\\main\\resources\\data\\sqlite\\sql.db";
			user = "";
			password = "";
		}else {
			driver = "com.mysql.jdbc.Driver";
			url = "jdbc:mysql://172.31.0.57:3308/dyda";
			user = "root";
			password = "hitdybyandrew";
		}
		Connection conn = null;
		try {
			System.out.println("whatDB:" + whatDB);
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password);
			System.out.println("GET CONN SUCCESS!");
			return conn;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

}
